package com.xinsoft.controller.basic;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.SysUserChangePasswordDTO;
import com.xinsoft.entity.dto.SysUserDTO;
import com.xinsoft.entity.dto.SysUserPageParam;
import com.xinsoft.service.SysUserService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> add(@RequestBody SysUserDTO sysUserDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysUserDTO);
        return Response.succeed(userService.addSysUser(sysUserDTO));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  SysUserDTO sysUserDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysUserDTO);
        return Response.succeed(userService.editSysUser(sysUserDTO));
    }

    /**
     * 获取用户
     */
    @GetMapping("/detail")
    public Response<?> detail(@Valid  @RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(userService.getSysUser(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@Valid @RequestBody List<Integer> ids) {
        userService.del(ids);
        return Response.succeed();
    }
    /**
     * 重置密码"
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/resetPassWord")
    public Response<?> resetPassWord(@Valid @RequestBody SysUserChangePasswordDTO sysUserChangePasswordDTO) {
        userService.resetPassWord(sysUserChangePasswordDTO);
        return Response.succeed();
    }
    /**
     * 启用
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/enable")
    public Response<?> enable(@Valid  @RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        userService.enable(id);
        return Response.succeed();
    }
    /**
     * 禁用
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/disable")
    public Response<?> disable(@Valid  @RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        userService.disable(id);
        return Response.succeed();
    }
    /**
     * 导入
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/import")
    @ResponseBody
    public Response<?> importExcel(@Valid @RequestParam("file") MultipartFile multipartFile) throws IOException {

        return Response.succeed( userService.importExcel(multipartFile));
    }

    @ApiOperation("导出")
    @PostMapping("/export")
    public Response<?> export(@Valid @RequestBody @ApiParam("查询参数") SysUserPageParam sysUserPageParam, HttpServletResponse response) throws Exception {
        List<SysUserDTO> sysUserDTOList = userService.findListByParam(sysUserPageParam);
        ExcelUtils.exportByEasypoi(sysUserDTOList, SysUserDTO.class, "用户管理", "用户管理", response, "用户管理");
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@Valid @RequestBody SysUserPageParam sysUserPageParam) {
        return Response.succeed(userService.findPageListByParam(sysUserPageParam));
    }

    /**
     * 不分页
     */
    @PostMapping("/list")
    public Response<?> list(@Valid @RequestBody SysUserPageParam sysUserPageParam) {
        return Response.succeed(userService.findListByParam(sysUserPageParam));
    }
}
